package com.micromall.common.payment;


import com.micromall.common.payment.dto.request.Body;
import com.micromall.common.payment.dto.request.GateWayReq;
import com.micromall.common.payment.dto.request.Head;
import com.micromall.common.payment.dto.request.Ips;
import com.micromall.common.utils.DateUtil;
import com.micromall.common.utils.MD5Util;
import com.micromall.common.utils.XMLUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Dante Fung on 2017/6/21.
 */
public class ISPaymentRequest {

    private static final Logger logger = LoggerFactory.getLogger(ISPaymentRequest.class);

    /**
     * <p>消息数字签名</p>
     * <p></p>body（<body>节点的xml文本值）+merCode（商户号）+key(商户数字证书)</p>
     * @param body
     * @return
     */
    public static String getMD5Digest(String body){
        String digest = MD5Util.getMD5String(body+PaymentConstant.MERCODE+PaymentConstant.MD5_CERTIFICATE);
        return digest;
    }

    /**
     * <p>消息唯一标识</p>
     */
    public static String getMsgId(){
        return  Long.toString(System.currentTimeMillis());
    }

    /**
     * <p>请求时间</p>
     * @return
     */
    public static String getReqDate(String formatPattern){
        Date date=new Date();
        DateFormat df=new SimpleDateFormat(formatPattern);
        return df.format(date);
    }

    public static String genPayRequestMessage(String orderNo, String goodNames, String memberNo, String chargeMoney){
        String requestBody = null;
        Ips ips = new Ips();
        GateWayReq gateWayReq = new GateWayReq();
        Head head = new Head();
        Body body = new Body();
        ips.setGateWayReq(gateWayReq);
        gateWayReq.setHead(head);
        gateWayReq.setBody(body);

        head.setVersion("");
        head.setMerCode(PaymentConstant.MERCODE);
        head.setMerName("");
        head.setAccount(PaymentConstant.ACCOUNT);
        head.setMsgId(ISPaymentRequest.getMsgId());
        head.setReqDate(ISPaymentRequest.getReqDate("yyyyMMddHHmmss"));// 时间戳格式

        body.setMerBillNo(orderNo);// 订单号
        body.setAmount(new BigDecimal(chargeMoney).setScale(2, BigDecimal.ROUND_DOWN).toString());
        body.setDate(ISPaymentRequest.getReqDate(DateUtil.WITHOUT_DELIMETER_DATE_FORMAT));
        body.setCurrencyType(PaymentConstant.CURRENCY_TYPE);
        body.setGatewayType(PaymentConstant.GatewayType.debitCard);
        body.setLang("");
        body.setMerchanturl(PaymentConstant.ORDER_PAY_RETURN_MERCHANTURL+"?memberNo="+memberNo);
        body.setFailUrl(PaymentConstant.ORDER_PAY_FAIL_URL);
        body.setAttach("");
        body.setOrderEncodeType(PaymentConstant.ORDER_ENCODE_TYPE);
        body.setRetEncodeType(PaymentConstant.RET_ENCODE_TYPE);
        body.setRetType(PaymentConstant.RET_TYPE);
        body.setServerUrl(PaymentConstant.SERVER_URL+"?memberNo="+memberNo);
        body.setBillEXP("");
        body.setGoodsName(goodNames);
        body.setIsCredit("");
        body.setBankCode("");

        String productType = PaymentConstant.STD_PAY_URL.equalsIgnoreCase(PaymentConstant.STD_PAY_URL)?"":PaymentConstant.ProductType.ENTERPRISE_ONLINE_BANK;
        body.setProductType(PaymentConstant.ProductType.PERSONAL_ONLINE_BANK);

        head.setSignature(ISPaymentRequest.getMD5Digest(XMLUtil.write2XML(body)));
        System.out.println(XMLUtil.write2XML(body));
        System.out.println(String.format("待MD5加密的字符串为：%s",XMLUtil.write2XML(body)+PaymentConstant.MERCODE+PaymentConstant.MD5_CERTIFICATE));
        System.out.println("md5数字签名："+head.getSignature());
        // -----------------------------------------------------------------
//        requestBody = XMLUtil.write2XML(ips);
        System.out.println("目标报文："+XMLUtil.write2XML(ips));
        if (logger.isDebugEnabled()) {
            logger.info(XMLUtil.write2XML(body));
            logger.info("md5数字签名："+head.getSignature());
            logger.info(String.format("待MD5加密的字符串为：%s",XMLUtil.write2XML(body)+PaymentConstant.MERCODE+PaymentConstant.MD5_CERTIFICATE));
            logger.info("目标报文："+XMLUtil.write2XML(ips));
        }

        return XMLUtil.write2XML(ips);
    }

    public static void main(String[] args) {
        ClassLoader classLoader = ISPaymentRequest.class.getClassLoader();
        System.out.println("路径为:"+classLoader.getResource("/"));
    }
}
